package com.pmms.util;

import java.security.SecureRandom;

public class AESEncryptionUtil {
    private static final int ALPHABET_SIZE = 26;
    private static final int NUMERIC_SIZE = 10;
    public static final int SecreteKey = 12;
    private static final int KEY_SIZE = 32;
    public static String generateToken() {
        SecureRandom secureRandom = new SecureRandom();
        byte[] keyBytes = new byte[KEY_SIZE];
        secureRandom.nextBytes(keyBytes);
        return bytesToHex(keyBytes);
    }

    // Helper method to convert byte array to hex string
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
    public static String encrypt(String plaintext, int key) {
        StringBuilder ciphertext = new StringBuilder();

        for (char ch : plaintext.toCharArray()) {
            if (Character.isLetter(ch)) {
                char base = Character.isUpperCase(ch) ? 'A' : 'a';
                char shiftedChar = (char) (base + (ch - base + key) % ALPHABET_SIZE);
                ciphertext.append(shiftedChar);
            } else if (Character.isDigit(ch)) {
                //char shiftedChar = (char) ('0' + (ch - '0' + key) % NUMERIC_SIZE);
                char shiftedChar =(char) ('0' + ((ch - '0' + key) % NUMERIC_SIZE + NUMERIC_SIZE) % NUMERIC_SIZE);
                ciphertext.append(shiftedChar);
            } else {
                ciphertext.append(ch);
            }
        }

        return ciphertext.toString();
    }

    public static String decrypt(String ciphertext, int key) {
        StringBuilder plaintext = new StringBuilder();

        for (char ch : ciphertext.toCharArray()) {
            if (Character.isLetter(ch)) {
                char base = Character.isUpperCase(ch) ? 'A' : 'a';
                char shiftedChar = (char) (base + (ch - base - key + ALPHABET_SIZE) % ALPHABET_SIZE);
                plaintext.append(shiftedChar);
            } else if (Character.isDigit(ch)) {
                //char shiftedChar = (char) ('0' + (ch - '0' - key + NUMERIC_SIZE) % NUMERIC_SIZE);
                char shiftedChar = (char) ('0' + ((ch - '0' - key + NUMERIC_SIZE) % NUMERIC_SIZE + NUMERIC_SIZE) % NUMERIC_SIZE);
                plaintext.append(shiftedChar);
            } else {
                plaintext.append(ch);
            }
        }

        return plaintext.toString();
    }

//    public static void main(String[] args) {
//        String token = generateToken();
//
//        System.out.println(token);
//        String t = encrypt(token,12);
//        System.out.println(t);
//        t =  decrypt(t,12);
//        System.out.println(t);
//
//    }

}
